
package cn.hpclub.server.service.promotion;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;

/**
 * @FileName: PromotionService.java
 * @Author zhl
 * @Description:首页弹窗信息Service
 * @Date 2016年6月27日 下午2:38:57
 * @CopyRight ZTE Corporation
 */
public class PromotionService{

    private static final Logger    logger  = LoggerFactory.getLogger(PromotionService.class);
    public static PromotionService service = new PromotionService();

    public String getSqlExceptSelect(ConcurrentHashMap<String, String> searchInfo){
        String sqlExceptSelect = " from home_page_promotion h " + " LEFT JOIN sys_role su ON h.creator = su.id "
                + "   where 1=1 and h.isdelete=0 ";

        for(Entry<String, String> item : searchInfo.entrySet()){
            sqlExceptSelect += buildQuery(item);
        }
        sqlExceptSelect += " order by h.create_time desc ";

        return sqlExceptSelect;
    }

    private String buildQuery(Entry<String, String> item){
        if("start_time".equalsIgnoreCase(item.getKey())){
            String start_time = "'" + item.getValue().replaceAll(" ", "") + "'";
            return " and h." + item.getKey() + " >= " + start_time + " ";
        }
        if("end_time".equalsIgnoreCase(item.getKey())){
            String end_time = "'" + item.getValue().replaceAll(" ", "") + "'";
            return " and h." + item.getKey() + " <= " + end_time + " ";
        }
        if("name".equalsIgnoreCase(item.getKey())){
            return " and h." + item.getKey() + " like '%" + item.getValue() + "%'";
        }
        if("org_id".equalsIgnoreCase(item.getKey())){
            return " and h.org_id in("
                    + InterfaceUtil.getChlidOrgIdSql(Integer.parseInt(item.getValue()), Constant.MAX_ORG_LEVEL, true)
                    + ") ";
        }
        return " and h." + item.getKey() + " = '" + item.getValue() + "'";
    }

}
